<?php
/**
 * Created by PhpStorm.
 * User: wx:wenyi2048
 * Date: 2022/3/19 0019
 * Time: 14:36
 */

namespace app\common\service;
use think\Db;
use think\Exception;
use think\Session;

class Service
{

    protected $db;

    protected $page = 1;//分页查找页

    protected $pageSize;

    protected $tableName;

    protected $param;

    protected $adminStore;//商家管理员对应店铺

    public function __construct($param = null)
    {
        $this->page = empty($param['page'])?1:$param['page'];
        $this->pageSize = empty($param['pageSize'])?config('listRows'):$param['pageSize'];//一页多少条记录
        $this->db = Db::table($this->tableName);
        $this->param = $param;

        //获取管理员账户
        $store = Session::get('store');
        if($store) $this->adminStore = json_decode($store,true);

    }

    public function setDb($tableName){
        $this->db = Db::table($tableName);
    }

    public function getAdminStoreId(){
        if(!$this->adminStore) throw  new Exception('店铺信息丢失，请重新登录');
        return $this->adminStore['id'];
    }

    public static function getAdminSelfStore(){
        //获取管理员账户
        $store = Session::get('store');
        if($store) return json_decode($store,true);
        return  [];
    }

    //放这里不对 应该放在stor
    public function getAdminStore($storeId){
        $checkId = $this->checkStoreSelect($storeId);//管理员可以随意选择
        if($checkId){
            return $this->db->where('id','=',$checkId)->find();
        }
        return $this->adminStore;
    }

    public function pageList(&$view,$param){
        $data = $this->db->paginate($this->pageSize,false, ['query' => $param]);
        $view->assign('list', $data);
        $view->assign("page", $data->render());
        $view->assign("count", $data->total());
        $view->assign('numPerPage', $this->pageSize);
        return $view;
    }

    public function searchPage(&$view,$params,$isdelete = 0){
        $this->db->where('isdelete','=',$isdelete)
            ->where('status','=',1)
            ->order('sort','asc');
        $this->pageList($view,$params);
    }


    /**
     * api 分页模糊查询
     */
    public function datalist(){
        $page = $this->page >= 1?($this->page - 1):0;
        $page = $page*$this->pageSize;

        $data = $this->db->limit($page,$this->pageSize)->select();
        $isEnd = count($data) == $this->pageSize ? false : true;
        return ['isend'=>$isEnd,'data'=>$data];
    }


    public function add(){
        unset($this->param['id']);
        return $this->db->insertGetId($this->param);
    }

    public function edit(){
        if(empty($this->param['id'])) apiError('参数丢失');
        $this->db->where('id','=',$this->param['id']);
        return $this->db->update($this->param);
    }

    public function setApiAuth(){

    }

    /**
     * 检查指定店铺查询，当是超管返回指定查询，当是店铺只能返回自家店铺id
     */
    public function checkStoreSelect($storeId){
        if(ADMINISTRATOR){//权限判断
            return $storeId;
        }else{
            return  $this->adminStore['id'];
        }
    }
}